/*
	Copyright 2010 Kwok Ho Yin

   	Licensed under the Apache License, Version 2.0 (the "License");
   	you may not use this file except in compliance with the License.
   	You may obtain a copy of the License at

    	http://www.apache.org/licenses/LICENSE-2.0

   	Unless required by applicable law or agreed to in writing, software
   	distributed under the License is distributed on an "AS IS" BASIS,
   	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   	See the License for the specific language governing permissions and
   	limitations under the License.
*/

package com.hykwok.CameraEffect;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class ActivityCameraSetting extends Activity {
	private static final String TAG = "ActivityCameraSetting";
	
	private MyCameraControl m_control;
	
	private ArrayAdapter<String>	m_antibanding;
    private ArrayAdapter<String>	m_coloreffects;
    private ArrayAdapter<String>	m_flashmodes;
    private ArrayAdapter<String>	m_focusmodes;
	private ArrayAdapter<String>   	m_pictureformat;
	private ArrayAdapter<String>	m_picturesize;
    private ArrayAdapter<String>   	m_previewformat;
	private ArrayAdapter<String>	m_previewsize;
    private ArrayAdapter<String>	m_previewframerate;
	private ArrayAdapter<String>	m_scenemodes;
	private ArrayAdapter<String>	m_whitebalance;
	
	private Spinner 				spinner_antibanding;
	private Spinner 				spinner_coloreffect;
	private Spinner 				spinner_flashmodes;
	private Spinner 				spinner_focusmodes;
	private Spinner 				spinner_pictureformat;
	private Spinner 				spinner_picturesize;
	private Spinner 				spinner_previewformat;
	private Spinner 				spinner_previewsize;
	private Spinner 				spinner_previewframerate;
	private Spinner 				spinner_scenemodes;
	private Spinner 				spinner_whitebalance;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        
        Log.d(TAG, "onCreate >>>>>");
        
        m_control = new MyCameraControl();
        
        setContentView(R.layout.camera_setting);
        
        // AnitBanding setting
        spinner_antibanding = (Spinner)findViewById(R.id.Spinner_Antibanding);
        
        if(m_control.m_camera_param.getSupportedAntibanding().size() > 0) {
        	m_antibanding = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_camera_param.getSupportedAntibanding());
        	m_antibanding.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        	
        	spinner_antibanding.setAdapter(m_antibanding);
        	spinner_antibanding.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_antibanding.setEnabled(false);
        }
        
        // Color Effect setting
        spinner_coloreffect = (Spinner)findViewById(R.id.Spinner_ColorEffects);
        
        if(m_control.m_camera_param.getSupportedAntibanding().size() > 0) {
        	m_coloreffects = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_camera_param.getSupportedColorEffects());
        	m_coloreffects.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        	
        	spinner_coloreffect.setAdapter(m_coloreffects);
        	spinner_coloreffect.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_coloreffect.setEnabled(false);
        }
        
        // Flash Modes setting
        spinner_flashmodes = (Spinner)findViewById(R.id.Spinner_FlashModes);
        
        if(m_control.m_camera_param.getSupportedAntibanding().size() > 0) {
        	m_flashmodes = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_camera_param.getSupportedFlashModes());
        	m_flashmodes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        	
        	spinner_flashmodes.setAdapter(m_flashmodes);
        	spinner_flashmodes.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_flashmodes.setEnabled(false);
        }
        
        // Focus Modes setting
        spinner_focusmodes = (Spinner)findViewById(R.id.Spinner_FocusModes);
        
        if(m_control.m_camera_param.getSupportedAntibanding().size() > 0) {
        	m_focusmodes = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_camera_param.getSupportedFocusModes());
        	m_focusmodes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        	
        	spinner_focusmodes.setAdapter(m_focusmodes);
        	spinner_focusmodes.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_focusmodes.setEnabled(false);
        }
        
        // Picture format
        spinner_pictureformat = (Spinner)findViewById(R.id.Spinner_PictureFormat);
        
        if(m_control.m_support_picture_format.size() > 0) {
        	m_pictureformat = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_support_picture_format);
        	m_pictureformat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        	spinner_pictureformat.setAdapter(m_pictureformat);
        	spinner_pictureformat.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_pictureformat.setEnabled(false);
        }
        
        // Picture Size
        spinner_picturesize = (Spinner)findViewById(R.id.Spinner_PictureSize);
        
        if(m_control.m_support_picture_size.size() > 0) {
        	m_picturesize = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_support_picture_size);
        	m_picturesize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        	spinner_picturesize.setAdapter(m_picturesize);
        	spinner_picturesize.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_picturesize.setEnabled(false);
        }
        
        // Preview format
        spinner_previewformat = (Spinner)findViewById(R.id.Spinner_PreviewFormat);
        
        if(m_control.m_support_preview_format.size() > 0) {
        	m_previewformat = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_support_preview_format);
        	m_previewformat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        	spinner_previewformat.setAdapter(m_previewformat);
        	spinner_previewformat.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_previewformat.setEnabled(false);
        }
        
        // Preview Size
        spinner_previewsize = (Spinner)findViewById(R.id.Spinner_PreviewSize);
        
        if(m_control.m_support_preview_size.size() > 0) {
        	m_previewsize = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_support_preview_size);
        	m_previewsize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        	spinner_previewsize.setAdapter(m_previewsize);
        	spinner_previewsize.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_previewsize.setEnabled(false);
        }
        
        // Preview Frame Rate
        spinner_previewframerate = (Spinner)findViewById(R.id.Spinner_PreviewFrameRate);
        
        if(m_control.m_support_preview_framerate.size() > 0) {
        	m_previewframerate = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_support_preview_framerate);
        	m_previewframerate.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        	spinner_previewframerate.setAdapter(m_previewframerate);
        	spinner_previewframerate.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_previewframerate.setEnabled(false);
        }
		
		// Scene mode
        spinner_scenemodes = (Spinner)findViewById(R.id.Spinner_SceneModes);
        
        if(m_control.m_support_preview_framerate.size() > 0) {
        	m_scenemodes = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_camera_param.getSupportedSceneModes());
        	m_scenemodes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        	spinner_scenemodes.setAdapter(m_scenemodes);
        	spinner_scenemodes.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_scenemodes.setEnabled(false);
        }
		
		// White balance
        spinner_whitebalance = (Spinner)findViewById(R.id.Spinner_WhiteBalance);
        
        if(m_control.m_support_preview_framerate.size() > 0) {
        	m_whitebalance = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_control.m_camera_param.getSupportedWhiteBalance());
        	m_whitebalance.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        	spinner_whitebalance.setAdapter(m_whitebalance);
        	spinner_whitebalance.setOnItemSelectedListener(spinner_select);
        } else {
        	spinner_whitebalance.setEnabled(false);
        }
        
        Log.d(TAG, "onCreate <<<<<");
    }
	
	@Override
	public void onStart() {
		Log.d(TAG, "onStart >>>>>");
		
		super.onStart();
		
		if(m_control.m_Selected_Antibanding > 0) {
			spinner_antibanding.setSelection(m_control.m_Selected_Antibanding);
		}
		
		if(m_control.m_Selected_ColorEffects > 0) {
			spinner_coloreffect.setSelection(m_control.m_Selected_ColorEffects);
		}
		
		if(m_control.m_Selected_FlashModes > 0) {
			spinner_flashmodes.setSelection(m_control.m_Selected_FlashModes);
		}
		
		if(m_control.m_Selected_FocusModes > 0) {
			spinner_focusmodes.setSelection(m_control.m_Selected_FocusModes);
		}
		
		if(m_control.m_Selected_PictureFormat > 0) {
			spinner_pictureformat.setSelection(m_control.m_Selected_PictureFormat);
		}
		
		if(m_control.m_Selected_PictureSize > 0) {
			spinner_picturesize.setSelection(m_control.m_Selected_PictureSize);
		}
		
		if(m_control.m_Selected_PreviewFormat > 0) {
			spinner_previewformat.setSelection(m_control.m_Selected_PreviewFormat);
		}
		
		if(m_control.m_Selected_PreviewSize > 0) {
			spinner_previewsize.setSelection(m_control.m_Selected_PreviewSize);
		}
		
		if(m_control.m_Selected_PreviewFrameRate > 0) {
			spinner_previewframerate.setSelection(m_control.m_Selected_PreviewFrameRate);
		}
		
		if(m_control.m_Selected_SceneMode > 0) {
			spinner_scenemodes.setSelection(m_control.m_Selected_SceneMode);
		}
		
		if(m_control.m_Selected_WhiteBalance > 0) {
			spinner_whitebalance.setSelection(m_control.m_Selected_WhiteBalance);
		}
		
		Log.d(TAG, "onStart <<<<<");
	}
	
	@Override
	public void onRestart() {
		Log.d(TAG, "onRestart >>>>>");
		
		super.onRestart();
		
		Log.d(TAG, "onRestart <<<<<");
	}
	
	@Override
	public void onResume() {
		Log.d(TAG, "onResume >>>>>");
		
		super.onResume();
		
		Log.d(TAG, "onResume <<<<<");
	}
	
	@Override
	public void onPause() {
		Log.d(TAG, "onPause >>>>>");
		
		super.onPause();
		
		m_control.updateParameters();
		
		Log.d(TAG, "onPause <<<<<");
	}
	
	@Override
	public void onStop() {
		Log.d(TAG, "onStop >>>>>");
		
		super.onStop();
		
		Log.d(TAG, "onStop <<<<<");
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy >>>>>");
		
		super.onDestroy();		
		
		m_control.Close();
		
		Log.d(TAG, "onDestroy <<<<<");
	}
	
	OnItemSelectedListener spinner_select = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			switch(parent.getId()) {
			case R.id.Spinner_Antibanding:
				m_control.m_Selected_Antibanding = position;
				break;
				
			case R.id.Spinner_ColorEffects:
				m_control.m_Selected_ColorEffects = position;
				break;
				
			case R.id.Spinner_FlashModes:
				m_control.m_Selected_FlashModes = position;
				break;
				
			case R.id.Spinner_FocusModes:
				m_control.m_Selected_FocusModes = position;
				break;
				
			case R.id.Spinner_PictureFormat:
				m_control.m_Selected_PictureFormat = position;
				break;
				
			case R.id.Spinner_PictureSize:
				m_control.m_Selected_PictureSize = position;
				break;
				
			case R.id.Spinner_PreviewFormat:
				m_control.m_Selected_PreviewFormat = position;
				break;
				
			case R.id.Spinner_PreviewSize:
				m_control.m_Selected_PreviewSize = position;
				break;
				
			case R.id.Spinner_PreviewFrameRate:
				m_control.m_Selected_PreviewFrameRate = position;
				break;
				
			case R.id.Spinner_SceneModes:
				m_control.m_Selected_SceneMode = position;
				break;
				
			case R.id.Spinner_WhiteBalance:
				m_control.m_Selected_WhiteBalance = position;
				break;
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}		
	};
}
